package com.github.xaidanwang.auth.server.util;

import com.alibaba.fastjson.JSON;
import com.github.xaidanwang.auth.server.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author wang yi fei
 * @date 2019/10/8 15:33
 */
@Component
public class RedisUtil {
	@Autowired
	private StringRedisTemplate redisTemplate;
	private final long timeout = 60;

	public void cacheToken(String token, UserInfo userInfo, long timeout){
		if (timeout == 0){
			timeout = this.timeout;
		}
		redisTemplate.opsForValue().set(token, JSON.toJSONString(userInfo),timeout, TimeUnit .SECONDS);
	}

	public void deleteToken(String token){
		redisTemplate.delete(token);
	}

}
